package com.nwpu.controller;

import com.nwpu.common.ErrorEnum;
import com.nwpu.common.Result;
import com.nwpu.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author DPH
 * @date 2023/5/26
 */
@RestController
@RequestMapping("/path")
public class PathController {

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping()
    public Result<String> getPath(@RequestParam String goodsId, Authentication authentication) {
        if (authentication == null || !authentication.isAuthenticated()) {
            return Result.error(ErrorEnum.FORBIDDEN);
        }
        // 获取用户信息并返回
        String userId = authentication.getName();
        String uuid = UUID.randomUUID().toString();
        String key = "SECKILL_PATH_" + userId + "_" + goodsId;
        redisUtil.set(key, uuid, 1);

        return Result.of(key);
    }
}
